package com.iotproject.vod.controller;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthResponse;
import com.iotproject.commonutils.R;
import com.iotproject.servicebase.exceptionhandler.GlobalException;
import com.iotproject.vod.service.VodService;
import com.iotproject.vod.utils.AliyunUtils;
import com.iotproject.vod.utils.ConstantVodUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/eduvod/video")
//d @CrossOrigin
public class VodController {
    @Autowired
    private VodService vodService;


    //上传视频到阿里云
    @PostMapping("uploadAlyiVideo")
    public R uploadAlyiVideo(MultipartFile file) {
        //返回上传视频id
        String videoId = vodService.uploadVideoAly(file);
        return R.ok().data("videoId", videoId);
    }

    //从阿里云删除视频
    @DeleteMapping("/removeAlyVideo/{id}")
    public R deleteAlyiVideo(@PathVariable("id") String id) {
        vodService.deleteVideoAly(id);
        return R.ok();
    }

//    @DeleteMapping("/hello/{id}")
//    public R hello(@PathVariable("id") String id) {
//        return R.ok().data("is", "ok");
//    }

    //根据视频id获取视频凭证
    @GetMapping("getPlayAuth/{id}")
    public R getPlayAuth(@PathVariable String id) {
        String accessKeyId = ConstantVodUtils.ACCESS_KEY_ID;
        String accessKeySecret = ConstantVodUtils.ACCESS_KEY_SECRET;
        DefaultAcsClient client = AliyunUtils.initVodClient(accessKeyId, accessKeySecret);
        GetVideoPlayAuthResponse response = new GetVideoPlayAuthResponse();
        try {
            //调用阿里云视频点播服务，获取视频凭证
            response = AliyunUtils.getVideoPlayAuth(client, id);
        } catch (Exception e) {
            e.printStackTrace();
            throw new GlobalException(20001, "获取凭证失败");
        }
        return R.ok().data("playAuth", response.getPlayAuth());
    }
 }
